package com.tokenplus.thai.coupons

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.tokenplus.thai.adata.coupons.ReqCouponUse
import com.tokenplus.thai.adata.coupons.ReqCouponVerify
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.aviewmodel.CouponsViewModel
import com.tokenplus.thai.coupons.adapter.CouponsAdapter
import com.tokenplus.thai.data.ReqBase
import com.tokenplus.thai.databinding.ActivityCouponsBinding
import com.tokenplus.thai.enums.PageTagEnum
import com.tokenplus.thai.home.repo.HomeCoinManager

/** coupons页 */
@SuppressLint("CustomSplashScreen")
class CouponsActivity : BaseActivity() {

    //布局binding
    private lateinit var mBinding: ActivityCouponsBinding

    //适配器
    private lateinit var mCouponsAdapter: CouponsAdapter

    //home viewModel
    private val mCouponsViewModel by lazy {
        ViewModelProvider(this)[CouponsViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mCouponsViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.coupons.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mBinding =
            ActivityCouponsBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //初始化刷新组件
            initRefreshUI()

            //监听返回
            onHandleLiveDataCallback()

            //初始化适配器
            mCouponsViewModel.mCouponList.clear()
            mCouponsAdapter = CouponsAdapter(mCouponsViewModel.mCouponList)
            mCouponsAdapter.setItemClickListener {
                mCouponsViewModel.couponUse(ReqCouponUse(it.id ?: ""))
            }

            mBinding.listCoupons.layoutManager = LinearLayoutManager(this)
            mBinding.listCoupons.adapter = mCouponsAdapter

            mBinding.couponCodeInput.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(editable: Editable?) {
                    val text = editable.toString()
                    if (text.isNotEmpty()) {
                        mBinding.couponCodeInpuDel.visibility = View.VISIBLE
                    } else {
                        mBinding.couponCodeInpuDel.visibility = View.GONE
                    }
                }

                override fun beforeTextChanged(
                    s: CharSequence,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            })

            mBinding.couponCodeInpuDel.setOnClickListener {
                mBinding.couponCodeInput.text = null
            }

            mBinding.couponCodeVerify.setOnClickListener {
                val code = mBinding.couponCodeInput.text.toString()
                if (code.isBlank()) {
                    mBinding.couponCodeError.visibility = View.VISIBLE
                    return@setOnClickListener
                }

                mBinding.couponCodeError.visibility = View.GONE
                mCouponsViewModel.couponVerify(ReqCouponVerify(code))
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()

        //获取coupons列表
        getCouponList()
    }

    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    private fun onHandleLiveDataCallback() {
        mCouponsViewModel.onGetCouponsLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    mCouponsViewModel.mCouponList.clear()
                    mCouponsViewModel.mCouponList.addAll(result)
                    mCouponsAdapter.notifyDataSetChanged()

                    if (mCouponsViewModel.mCouponList.size > 0) {
                        mBinding.emptyLayout.llEmptyList.visibility = View.GONE
                    } else {
                        mBinding.emptyLayout.llEmptyList.visibility = View.VISIBLE
                    }
                }
            }
        }

        mCouponsViewModel.onCouponUseLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                getCouponList()
            }
        }

        mCouponsViewModel.onCouponVerifyLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                getCouponList()
            }
        }
    }

    /** 获取coupons列表 */
    private fun getCouponList() {
        HomeCoinManager.mGlobalCoinTypeLiveData.value?.let {
            val reqBase = ReqBase()
            reqBase.showLoading = false
            mCouponsViewModel.getCoupons(reqBase)
        }
    }

    /** 初始化下拉刷新 */
    private fun initRefreshUI() {
        connectSwipeRefreshLayout(mBinding.swipeRefresh) {
            getCouponList()
        }
    }
}